class ChairmansController < ApplicationController
  before_filter :authenticate
  # GET /events
  # GET /events.xml
  def index
    @page = params[:page].nil? ? 1 : params[:page]
    # @events = Event.all.paginate :page=>params[:page], :order=>'created_at desc', :per_page => 5
    @filter = params[:filter].nil? ? {} : params[:filter]
    @events = Event.filter(@filter).paginate :page=>params[:page], :order=>'created_at desc', :per_page => 5
    
    @user = current_user
    
    respond_to do |format|
      format.html # index.html.erb
    end
  end
  
  def show
    @event = Event.find(params[:id])
    
    @articles = @event.articles.paginate :page=>params[:page], :order=>'created_at desc', :per_page => 2
    # @articles = @event.articles.paginate :page=>(params[:page].to_i - 1), :order=>'created_at desc', :per_page => 2 if @articles.count == 0 && !params[:page].nil?
    # @page = params[:page].nil? ? 1 : params[:page]
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @event }
      format.js {render 'articles.js.erb'}
    end
  end
  
  def new
    @event = Event.new

    # city, areas
    @cities = City.all.collect {|p| [ p.name, p.id ] }
    @areas = City.first.areas.all.collect {|p| [ p.name, p.id ] }

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @event }
    end
  end
  
  def edit
    @event = Event.find(params[:id])
    #@event = @article.event if !@article.event.nil?
    
    # location & city
    @location = @event.locations.last
    @city = @location.area.city
    
    # cities, areas
    @cities = City.all.collect {|p| [ p.name, p.id ] }
    @areas = @city.areas.all.collect {|p| [ p.name, p.id ] }   
    
    
    
    @user = current_user
    
  end
  
  def save
    if !params[:id].nil?
      # FIND EXIST EVENT
      @event = Event.find(params[:id])
      @user = current_user
      # test if status is APPROVING
      if !@event.chairman_edit?(@user)
        respond_to do |format|
          format.html {
            flash[:error] = "Event can't be edited because its status is: ASSIGNED."
            redirect_to({:controller => 'chairmans'})
                      }
          format.xml  { head :ok }
        end
      else
        
      # location
      @location = Location.find(@event.locations.last.id)
      @location.update_attributes(params[:location])
      
        respond_to do |format|
          if @event.update_attributes(params[:event])
            format.html { redirect_to({:action => 'index'}, :notice => 'Event was successfully updated.') }
            format.xml  { head :ok }
          else
            format.html { render :action => "edit" }
            format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
          end
        end
      end      
    else
    # CREATE NEW EVENT
      @event = Event.new(params[:event])
      
      # location
      @location = Location.new(params[:location])
      @event.location = @location
      
      
      # CHANGE STATUS
      @event.set_status('NEW') # article_status = ArticleStatus.get('NEW')
      
      # Add current user
      @event.user = current_user
      
      respond_to do |format|
        if @event.save
          format.html { redirect_to({:action => 'index'}, :notice => 'Event was successfully updated.') }
          format.xml  { render :xml => @event, :status => :created, :location => @event }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @event.errors, :status => :unprocessable_entity }
        end
      end
    end
  end
  
  def destroy
    
    @user = current_user
    @event = Event.find(params[:id])
    
    if !@event.chairman_edit?  (@user)      
        respond_to do |format|
          format.html {
            flash[:error] = "Event can't be deleted because its status is: ASSIGNED."
            redirect_to({:action => 'index'})
          }
          format.xml  { head :ok }
          format.js { render :text => 'error' }
        end
        return nil
    else
      # destroy article
      @event.destroy
      respond_to do |format|
          format.html {
            redirect_to({:action => 'index'}, :notice => 'Event was successfully deleteed')
          }
          format.xml  { head :ok }
          format.js { render :text => 'error' }
        end
    end
  end
  
  def delete_events
    @ids = params[:ids]
    @user = current_user
    
    @ids.each do |obj|
      @event = Event.find(obj[0])
      @event.destroy
    end
    
    
    respond_to do |format|
        format.html { redirect_to({:action => 'index'}, :notice => "Events was successfully deleted.") }
        format.xml  { head :ok }
        format.js { render 'destroy.js.erb' }
    end
  end
  
  def assign_form
    @events = Event.all.collect {|p| [ p.title, p.id ] }
  end
  
  def published
    @event = Event.find(params[:id])
    
    if @event.published
      @event.published = false
      @turn = "off"
      @do = "un"
      @title = "No"
    else
      @event.published = true
      @turn = "on"
      @do = ""
      @title = "Yes"
    end
    
    respond_to do |format|
      if @event.save
        format.html { redirect_to(events_url, :notice => "Article was successfully #{@do}published.") }
        format.js { render 'published.js.erb' }
      else
        format.html { redirect_to(events_url, :notice => "Article didn\'t been #{@do}published.") }
        format.js { render 'published_err.js.erb' }
      end
    end
      
  end 
end
